package cn.kgc.easybuy.service.impl;

import cn.kgc.easybuy.dao.NewsMapper;
import cn.kgc.easybuy.pojo.News;
import cn.kgc.easybuy.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public boolean delNews(Integer id) {
        int flag = newsMapper.delNews(id);
        if (flag == 1){
            return true;
        }else {
            throw new RuntimeException("删除资讯:"+id+"失败");
        }
    }

    @Override
    public boolean updateNews(News news) {
        int flag = newsMapper.updateNews(news);
        if (flag == 1){
            return true;
        }else {
            throw new RuntimeException("修改资讯:"+news.getTitle()+"失败");
        }
    }

    @Override
    public boolean addNews(News news) {
        news.setIsdelete(0);
        news.setCreateTime(new Date());
        int flag = newsMapper.addNews(news);
        if (flag == 1){
            return true;
        }else {
            throw new RuntimeException("添加资讯:"+news.getTitle()+"失败");
        }
    }

    @Override
    public News selectNewsById(Integer id) {
        News news = newsMapper.selectNewsById(id);
        if (news!=null){
            return news;
        }else {
            throw new RuntimeException("查询资讯:"+id+"失败");
        }
    }

    @Override
    public PageInfo<News> selectNewsByPage(Map<String, Object> params) {
        PageHelper.startPage(params);
        List<News> list = newsMapper.selectNewsByPage(params);
        return new PageInfo<News>(list);
    }
}
